﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using BaseEntities;

namespace DataAccess
{
    public class CardDataAccess
    {
        public static bool ValidateCard(Card c)
        {
            SqlConnection cn = ConnectDB.Getconnect();
            DataTable dt = new DataTable();
            try
            {
                SqlCommand cmd = new SqlCommand("ValidateCard", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@CardNo", SqlDbType.NVarChar).Value = c.CardNo;
                cn.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                cn.Close();
                if (dt.Rows.Count == 1)
                {
                    return true;
                }
                else
                    return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static bool ValidatePIN(Card c)
        {
            SqlConnection cn = ConnectDB.Getconnect();
            DataTable dt = new DataTable();
            try
            {
                SqlCommand cmd = new SqlCommand("ValidatePIN", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@CardNo", SqlDbType.NVarChar).Value = c.CardNo;
                cmd.Parameters.Add("@PIN", SqlDbType.NVarChar).Value = c.PIN;
                cn.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                cn.Close();
                if (dt.Rows.Count == 1)
                {
                    return true;
                }
                else
                    return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static bool UpdateCard(Card c)
        {
            SqlConnection cn = ConnectDB.Getconnect();
            DataTable dt = new DataTable();
            try
            {
                SqlCommand cmd = new SqlCommand("BlockCard", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@CardNo", SqlDbType.NVarChar).Value = c.CardNo;
                cn.Open();
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
